<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
    <title>购物车</title>
</head>
<body>
<?php 
include_once("header.php");
check_loginuser();
$tb_name = "cart";
$page = $_REQUEST["page"]?$_REQUEST["page"]:"1";
$whereSql = " userid=".$_SESSION["id"];
if ($_REQUEST["usersid"]){
	$whereSql .= " and usersid=" . $_REQUEST["usersid"];
}
$list = db_get_all("select * from $tb_name where $whereSql order by id desc");
$cat_title = "购物车";
// if($_REQUEST["act"]=="yes"){
// 	db_dela($tb_name,"userid=".$_SESSION["id"]);
// 	goBakMsg("清空成功");
// }
?>
<div class="m_ctr">
    <div class="ej_tctr">
		当前位置：
		<a href="index.php">首页</a> &gt; <?php echo $cat_title;?>
	</div>
    <div class="m_ctr1">
        <div class="">
            <div class="ej_rt3">我的购物车</div>
			<link type="text/css" rel="stylesheet" href="./Public/css/list.css">
            <form action='cart2.php' method='post'>
				<input type="hidden" name="usersid" value="<?php echo $_REQUEST["usersid"];?>">
				<div class="listmain">
					<div class="cart_table">
						<table width="100%" border="0" cellpadding="0" cellspacing="0">
							<tbody>
								<tr class="tr_top">
									<td width="48">
										<input type="checkbox"  class="cartcheckbox" id="cartcheckbox" checked="true" />
									</td>
									<td width="">商品</td>
									<td width="143">单价（元）</td>
									<td width="143">数量</td>
									<td width="143">小计（元）</td>
									<td width="80">操作</td>
								</tr>
								<?php
								$total=0;
								foreach($list as $row) {
								$good = db_get_row("select * from goods where id=". $row["goodsid"] ."");
								?>
								<tr class="tr_view">
									<td>
										<input type="hidden" name="order_no" value="">
										<input type="checkbox" name="cart_id[]" class="cartcheckbox" checked="true" value='<?php echo $row['id'];?>'/>
									</td>
									<td class="cart_td1">
										<a href="goodsshow.php?id=<?php echo $good['id'];?>&categoryid=<?php echo $good['categoryid'];?>" class="cart_t">
											<img src="./Public/Upload/<?php echo $good["img"];?>">
											</a><span style="line-height: 65px;"><i><?php echo $good["title"];?></i></span>
									</td>
									<td class="cart_td2">￥<span><?php echo $good["sprice"];?></span></td>
									<td>
										<div class="cartnum">
											<span class="min">-</span><span goodsid='<?php echo $good['id'];?>' class="carttext"><?php echo $row['sums'];?></span>
											<span class="add">+</span>
										</div>
									</td>
									<td class="cart_td4 cartred">￥<span></span></td>
									<td>
										<a href="?id=<?php echo $row['id'];?>&act=del">删除</a></td>
									</td>
								</tr>
								<?php
								$total=$total+1;
								}?>
							</tbody>
						</table>
					</div>
					<p class="carttotal">
						共 <i class="cartred"><?php echo $total;?></i> 件商品，总计金额：
						<span class="cartred">¥<em></em></span>
					</p>
					<div class="cartbtn">
						<input type="submit" value="确认无误，结算"><a href="goods.php">继续购物</a>
					</div>
				</div>
			</form>
			<script type="text/javascript">
				$(function() {
					// 点击全选按钮
					$("#cartcheckbox").click(function() {
						if ($(this).prop("checked") == true) {
							$(".cartcheckbox").prop("checked", true);
						} else {
							$(".cartcheckbox").prop("checked", false);
						}
					});
		
				function numInit(){
					var priceItem = new Array(); // 用来存放每行的小计;
					var sumCount = 0.00; // 用来存放总计;
					$('.tr_view').each(function(index, element){
						var num = parseInt($(element).find(".carttext").text());
						var price = parseFloat($(element).closest('tr').find('.cart_td2 span').text());
						var count = (num*price).toFixed(2);
						$(element).find(".cart_td4 span").text(count);
						if($(element).find('.cartcheckbox').prop('checked')){
							priceItem.push(count);
						}
				});
				for (var i=0; i<priceItem.length; i++) {
					sumCount += parseFloat(priceItem[i]);
				}
				if(parseFloat(sumCount)===0.00){
					$('.cartbtn input').css({'background':'#ccc', 'color':'#999'}).attr('disabled', 'disabled');
				} else{
					// 如果总计不为零，则移除disabled属性
					$('.cartbtn input').css({'background':'#b13f3f', 'color':'#fff'}).removeAttr('disabled');
				}
				$(".carttotal em").text(sumCount.toFixed(2));
				}
				numInit();
				 // 加减数
				// 加
				$(".add").click(function() {
				    var num = parseInt($(this).siblings(".carttext").text());
				    var goodsid = parseInt($(this).siblings(".carttext").attr('goodsid'));
				    htmlobj=$.ajax({type: 'POST',url: "cartajax.php?a=check_count",data: {goodsid:goodsid,count:num},dataType: 'json',async:false});  
				    if(htmlobj.responseText==1){
						$(this).siblings(".carttext").text(num+1);
						numInit();
				    }
				    else{
						alert('库存不足');
					}
				})
				// 减
				$(".min").click(function() {
				    var num = parseInt($(this).siblings(".carttext").text());
				    var goodsid = parseInt($(this).siblings(".carttext").attr('goodsid'));
				    if(num == 0){
				        return;
				    }
				    htmlobj=$.ajax({type: 'POST',url: "cartajax.php?a=ajaxBuyCount",data: {goodsid:goodsid,count:num},dataType: 'json',async:false}); 
				    if(htmlobj.responseText=='ok'){
				    $(this).siblings(".carttext").text(num-1);
				    numInit();
				    }     
				})
				// 取消选中则重新计算总价
				$(".cartcheckbox").change(function(){
					numInit();
				});
				function check_count(goodsid,count)
				{
					var data={goodsid:goodsid,count:count};
					var htmlobj=$.ajax({type: 'POST',url: '{:U("Ajax/check_count")}',data: data,dataType: 'json',async:false});
					return( htmlobj.responseText);
				}
        
			})
			</script>
        </div>
    </div>
</div>
<?php
if ($_REQUEST["act"]=="del") {
		db_del($tb_name,$_REQUEST["id"]);
		urlMsg("27操作成功", $tb_name.".php");
	}
	include_once("footer.php");
?>
</body>
</html>